import random
from ronglian_sms_sdk import SmsSDK
from django.conf import settings
import json

def generate_verify_code():
    """生成6位数字验证码"""
    return ''.join(random.sample('0123456789', 4))
def send_sms_by_sdk(phone, code):
    """
    发送短信验证码
    :param phone: 手机号
    :param code: 验证码
    :return: 是否发送成功 (bool)
    """
    sdk = SmsSDK(
        settings.RONG_LIAN["ACCOUNT_SID"],
        settings.RONG_LIAN["AUTH_TOKEN"],
        settings.RONG_LIAN["APP_ID"]
    )
    # 短信模板参数（需与模板中{1}对应，此处为验证码）
    datas = (code, "2")  # 3分钟有效期，与模板提示一致
    resp = sdk.sendMessage(
        settings.RONG_LIAN["TEMPLATE_ID"],
        phone,
        datas
    )
    resp_dict = json.loads(resp)
    # 容联云返回码：0为成功
    return resp_dict.get("statusCode") == "0"